#include <ansi_c.h>
#include <cvirte.h>		
#include <userint.h>
#include "grap.h"
#define SIZE 9
#define nr 'n'
#define bl 'b'
#define ab ' '
#define X 'x'
#define tmp 'X'
/*--------------------------------------------------------------------------------------------------------------------------------------------*/
														/*PROTOTYPE DE RETOURNE PION N/B DES FONCTIONS*/
void bl_return_pion(int,int,int);
void nr_return_pion(int,int,int);
/*--------------------------------------------------------------------------------------------------------------------------------------------*/
														/*PROTOTYPE DETECTION N/B DES FONCTIONS*/  
void bl_detection(int tab[SIZE][SIZE]);
void nr_detection(int tab[SIZE][SIZE]);
/*--------------------------------------------------------------------------------------------------------------------------------------------*/
													   /*VARIABLES GENERALE*/
static int image;
static int noir;
static int blanc;
static int post;
int tab[SIZE+1][SIZE]= { {ab,'1','2','3','4','5','6','7','8'},
						  {'1',ab,ab,ab,ab,ab,ab,ab,ab},
 				 		  {'2',ab,ab,ab,ab,ab,ab,ab,ab},		
 						  {'3',ab,ab,ab,ab,ab,ab,ab,ab},
 				 		  {'4',ab,ab,ab,bl,nr,ab,ab,ab},
						  {'5',ab,ab,ab,nr,bl,ab,ab,ab},
						  {'6',ab,ab,ab,ab,ab,ab,ab,ab},
 					      {'7',ab,ab,ab,ab,ab,ab,ab,ab},
						  {'8',ab,ab,ab,ab,ab,ab,ab,ab},
					  };
int colonne;
int ligne;
int k=0;
/*--------------------------------------------------------------------------------------------------------------------------------------------*/
static int panelHandle;

int main (int argc, char *argv[])
{
	int i;
	int j;
	static Point cell;
	for(i=0;i<SIZE;i++)				 
     	{										 
     	for(j=0;j<SIZE;j++)				  
     	{	
     		if(tab[i][j]==ab)
     		{
     		 cell.y=i;
     		 cell.x=j;
     		 GetBitmapFromFile ("c:\\Documents and Settings\\thomas.de-guigne\\Bureau\\othello2013\\interface_graphique\\photo\\tableaux.jpg", &image);
     		}
     		if(tab[i][j]==nr)
     		{
     		cell.x=j;
     		cell.y=i;
     		GetBitmapFromFile ("c:\\Documents and Settings\\thomas.de-guigne\\Bureau\\othello2013\\interface_graphique\\photo\\pion_noir.gif", &noir); 
     		}
     		if(tab[i][j]==bl)
     		{
     		cell.y=i;
     		cell.x=j;
     		GetBitmapFromFile ("c:\\Documents and Settings\\thomas.de-guigne\\Bureau\\othello2013\\interface_graphique\\photo\\pion_blanc.gif", &blanc);
     		}
     		if(tab[i][j]==X)
     		{
     		cell.x=j;
     		cell.y=i;
			GetBitmapFromFile ("c:\\Documents and Settings\\thomas.de-guigne\\Bureau\\othello2013\\interface_graphique\\photo\\soldat.gif", &post);
     		}
    	}
     	}
	if (InitCVIRTE (0, argv, 0) == 0)
		return -1;	/* out of memory */
	if ((panelHandle = LoadPanel (0, "grap.uir", PANEL)) < 0)
		return -1;
	DisplayPanel (panelHandle);
	RunUserInterface ();
	DiscardPanel (panelHandle);
	return 0;
}

int CVICALLBACK fermer_prog (int panel, int event, void *callbackData,
		int eventData1, int eventData2)
{
	switch (event)
	{
		case EVENT_CLOSE:
			QuitUserInterface (0);
			break;
	}
	return 0;
}

int CVICALLBACK table (int panel, int control, int event,
		void *callbackData, int eventData1, int eventData2)
{
	static Point cell;
	int i;
	int j;
	int pion_b=0;
    int pion_n=0;
    int pion_x=0;
	int  save;
	int Joueur = bl;
	
	switch (event)
	{
		case EVENT_COMMIT:
		QuitUserInterface (0);
		break;
		case EVENT_LEFT_CLICK:
	ligne = cell.y;
	colonne = cell.x;
	save = tab[ligne][colonne];
	if(Joueur == bl)
	{
     if(save == X)
     {
      tab[ligne][colonne] = tmp ;
      bl_return_pion(tab[ligne][colonne],ligne,colonne);
      Joueur = nr ;
     }
    }
	else
	{
	 if(save == X)
     {
      tab[ligne][colonne] = tmp ;
      nr_return_pion(tab[ligne][colonne],ligne,colonne);
      Joueur = bl ;
     }
  	GetActiveTableCell (PANEL, PANEL_TABLE, &cell);
	GetBitmapFromFile ("c:\\Documents and Settings\\thomas.de-guigne\\Bureau\\othello2013\\interface_graphique\\photo\\pion_noir.gif", &noir);
	GetBitmapFromFile ("c:\\Documents and Settings\\thomas.de-guigne\\Bureau\\othello2013\\interface_graphique\\photo\\pion_blanc.gif", &blanc);
	GetBitmapFromFile ("c:\\Documents and Settings\\thomas.de-guigne\\Bureau\\othello2013\\interface_graphique\\photo\\tableaux.jpg", &image);
	SetTableCellVal (PANEL, PANEL_TABLE, cell, image);
	}
				break;
		case EVENT_ACTIVE_CELL_CHANGE:
			break;
	}
	return 0;
}
